import { CmdUpTabs, FeatureManager, useCommandManager } from '@amcax/base';

import {
  CmdButton,
  CmdDivider,
  CmdTabItem,
  CmdTabs,
} from '@app-cad/components/CommandBar';

// Common CMD
import { useDocumentConsumer } from '@app-base/common/providers/documentProvider';
import { cmdPlane } from '@app-base/environments/nextcad/commands/curved/cmd-plane';
import { cmdGenerateDrafting } from '@app-base/environments/nextcad/commands/tools/cmd-generate-drafting';
import { cmdCircularArray } from '@app-cad-env/product/commands/cmd-array-circle';
import { cmdLinearArray } from '@app-cad-env/product/commands/cmd-array-linear';
import { cmdConstrain } from '@app-cad-env/product/commands/cmd-constrain';
import { cmdConstrainFix } from '@app-cad-env/product/commands/cmd-constrain-fix';
import { cmdCopyPart } from '@app-cad-env/product/commands/cmd-copy';
import { cmdInterferenceCheck } from '@app-cad-env/product/commands/cmd-interference-check';
import { cmdMirror } from '@app-cad-env/product/commands/cmd-mirror';
import { cmdTransformPart } from '@app-cad-env/product/commands/cmd-transform';
import { cmdImport } from '@app-cad/common/commands/cmd-import';
import { cmdRedo } from '@app-cad/common/commands/cmd-redo';
import { cmdSettings } from '@app-cad/common/commands/cmd-settings';
import { cmdUndo } from '@app-cad/common/commands/cmd-undo';
import { useMemo } from 'react';
import { cmdMeasure } from '../../../../common/commands/cmd-measure';
import { CmdBracketCatia } from '../../commands/bracket/cmd-bracket-catia';
import { CmdBracketExamines } from '../../commands/bracket/cmd-bracket-examines';
import { CmdBracketInstall } from '../../commands/bracket/cmd-bracket-install';
import { cmdAddDoc } from '../../commands/cmd-add';
import { cmdCopyWithConstraint } from '../../commands/cmd-copy-with-constraints';
import { cmdExchangeDoc } from '../../commands/cmd-exchange';
import { cmdExport } from '../../commands/cmd-export';
import { cmdHoleAlignCheck } from '../../commands/cmd-hole-align-check';
import { cmdInsertDoc } from '../../commands/cmd-insert';
import { cmdIntervalCheck } from '../../commands/cmd-interval-check';
import { cmdCsys } from '@app-base/common/commands/cmd-csys';

const BRACKET_SWITCH = false;
export default function CommandBar(props: any) {
  const { resetCmd } = useCommandManager();
  const { style: cmdStyle } = props;
  const { documentInfo } = useDocumentConsumer();
  const tabItems: CmdTabItem[] = useMemo(() => {
    const tbs = [
      {
        id: 'standard',
        label: '标准',
        children: (
          <>
            {FeatureManager.isFeatureEnabled('product-import') && (
              <CmdButton cmd={cmdImport} />
            )}
            {FeatureManager.isFeatureEnabled('product-export') && (
              <CmdButton cmd={cmdExport} />
            )}
            {FeatureManager.isFeatureEnabled('generate-drafting') && (
              <CmdButton cmd={cmdGenerateDrafting} />
            )}

            <CmdButton
              featureEnableCode='cmd-standard-setting'
              cmd={cmdSettings}
            />
            <>
              <CmdButton cmd={cmdUndo} />
              <CmdButton cmd={cmdRedo} />
            </>
          </>
        ),
      },
      {
        id: 'assembly',
        label: '装配',
        children: (
          <>
            {FeatureManager.isFeatureEnabled('product-insert') && (
              <CmdButton cmd={cmdInsertDoc} />
            )}
            {FeatureManager.isFeatureEnabled('product-add') && (
              <CmdButton cmd={cmdAddDoc} />
            )}
            {FeatureManager.isFeatureEnabled('product-exchange') && (
              <CmdButton cmd={cmdExchangeDoc} />
            )}
            <CmdDivider />
            {FeatureManager.isFeatureEnabled('product-copy') && (
              <CmdButton cmd={cmdCopyPart} />
            )}
            {FeatureManager.isFeatureEnabled('product-linear-array') && (
              <CmdButton cmd={cmdLinearArray} />
            )}
            {FeatureManager.isFeatureEnabled('product-circular-array') && (
              <CmdButton cmd={cmdCircularArray} />
            )}
            {FeatureManager.isFeatureEnabled('product-mirror') && (
              <CmdButton cmd={cmdMirror} />
            )}
            {FeatureManager.isFeatureEnabled(
              'product-copy-with-constraints',
            ) && <CmdButton cmd={cmdCopyWithConstraint} />}
            <CmdDivider />
            {FeatureManager.isFeatureEnabled('product-constrain-fix') && (
              <CmdButton cmd={cmdConstrainFix} />
            )}
            {FeatureManager.isFeatureEnabled('product-constrain') && (
              <CmdButton cmd={cmdConstrain} />
            )}
            {FeatureManager.isFeatureEnabled('product-transform') && (
              <CmdButton cmd={cmdTransformPart} />
            )}
            <CmdButton
              featureEnableCode='product-plane'
              cmd={cmdPlane}
            />
            <CmdButton
              featureEnableCode='product-csys'
              cmd={cmdCsys}
            />
          </>
        ),
      },
      {
        id: 'evaluation',
        label: '评估',
        children: (
          <>
            {FeatureManager.isFeatureEnabled('product-interference-check') && (
              <CmdButton cmd={cmdInterferenceCheck} />
            )}
            {FeatureManager.isFeatureEnabled('product-interval-check') && (
              <CmdButton cmd={cmdIntervalCheck} />
            )}
            {FeatureManager.isFeatureEnabled('product-hole-align-check') && (
              <CmdButton cmd={cmdHoleAlignCheck} />
            )}
            {FeatureManager.isFeatureEnabled('product-measure') && (
              <CmdButton cmd={cmdMeasure} />
            )}
          </>
        ),
      },
    ];
    if (documentInfo.type === 'bracket' || BRACKET_SWITCH) {
      tbs.push({
        id: 'bracket-smart-design',
        label: '支架智能设计',
        children: (
          <>
            <CmdButton
              featureEnableCode='bracket-install'
              cmd={CmdBracketInstall}
            />
            <CmdButton
              featureEnableCode='bracket-exam'
              cmd={CmdBracketExamines}
            />
            <CmdButton
              featureEnableCode='bracket-r-catia'
              cmd={CmdBracketCatia}
            />
          </>
        ),
      });
    }
    return tbs;
  }, [documentInfo.type]);

  const onTabSwitch = async () => {
    resetCmd();
  };

  if (cmdStyle === 'down') {
    return (
      <CmdTabs
        items={tabItems}
        onTabSwitch={onTabSwitch}
      />
    );
  }

  return (
    <CmdUpTabs
      items={tabItems}
      onTabSwitch={onTabSwitch}
    />
  );
}
